package com.suray.wcs.web.system.controller;

import com.suray.commin.annotation.Log;
import com.suray.commin.core.controller.BaseController;
import com.suray.commin.core.controller.ResponseData;
import com.suray.commin.core.page.TableDataInfo;
import com.suray.commin.core.utils.ExcelUtil;
import com.suray.commin.enmus.BusinessType;
import com.suray.wcs.service.system.entity.RgvHeartLogDB;
import com.suray.wcs.service.system.service.impl.RgvHeartLogServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/rgvHeartLog")
@CrossOrigin(value = "*", allowCredentials = "true")
public class RgvHeartLogController extends BaseController {

    @Autowired
    private RgvHeartLogServiceImpl rgvHeartLogService;

    /**
     * 分页查询
     *
     * @param rgvHeartLogdb
     * @return
     */
    @PreAuthorize("@ss.hasPermi('rgvHeartLog:query')")
    @ApiOperation(value = "查询心跳日志")
    @GetMapping(value = "/getRgvHeartLog")
    @ResponseBody
    public TableDataInfo getRgvHeartLog(RgvHeartLogDB rgvHeartLogdb) {
        startPage();
        List<RgvHeartLogDB> rgvHeartLogs = rgvHeartLogService.getRgvHeartLogs(rgvHeartLogdb);
        return getDataTable(rgvHeartLogs);
    }

    /**
     * 查询图表数据
     *
     * @param rgvHeartLogdb
     * @return
     */
    @PreAuthorize("@ss.hasPermi('rgvHeartLog:query')")
    @ApiOperation(value = "查询图表数据")
    @PostMapping(value = "/getGraphData")
    @ResponseBody
    public ResponseData getGraphData(RgvHeartLogDB rgvHeartLogdb) {
        try {
            Date sendTime = rgvHeartLogdb.getSendTime();
            Date sendTimeStart;
            Date sendTimeEnd;
            if (sendTime == null) {
                sendTimeEnd = new Date();
            } else {
                sendTimeEnd = sendTime;
            }
            sendTimeStart = DateUtils.addHours(sendTimeEnd, -1);
            rgvHeartLogdb.setSendTimeStart(sendTimeStart);
            rgvHeartLogdb.setSendTimeEnd(sendTimeEnd);
            List<Map<String, Object>> datalist = rgvHeartLogService.getGraphData(rgvHeartLogdb);
            return ResponseData.success("查询成功", datalist);
        } catch (Exception e) {
            return ResponseData.error(e.getMessage());
        }
    }

    @Log(title = "操作日志", method = "导出小车心跳日志", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('rgvHeartLog:export')")
    @GetMapping("/export")
    public ResponseData export(RgvHeartLogDB rgvHeartLogDB) {
        List<RgvHeartLogDB> list = rgvHeartLogService.getRgvHeartLogs(rgvHeartLogDB);
        ExcelUtil<RgvHeartLogDB> util = new ExcelUtil<>(RgvHeartLogDB.class);
        return util.exportExcel(list, "操作日志");
    }

    @PreAuthorize("@ss.hasPermi('rgvHeartLog:remove')")
    @Log(title = "操作日志", method = "删除小车心跳日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{interfaceLogIds}")
    public ResponseData remove(@PathVariable Long[] interfaceLogIds) {
        return toAjax(rgvHeartLogService.deleteLogByIds(interfaceLogIds));
    }

    @Log(title = "操作日志", method = "清空小车心跳日志", businessType = BusinessType.CLEAN)
    @PreAuthorize("@ss.hasPermi('rgvHeartLog:remove')")
    @DeleteMapping("/clean")
    public ResponseData clean() {
        rgvHeartLogService.cleanLog();
        return ResponseData.success();
    }
}
